'use strict';
var clientApp = angular.module('clientApp');

clientApp
    .controller('CmsStoreCtrl', ['$rootScope', '$scope', '$routeParams', '$timeout',
            'Constant', 'StringUtil', 'DomUtil', 'ThemeUtil', 'TemplateOpt',
            'StoreOpt', 'Category', 'ProductOpt', 'Product',
    function ($rootScope, $scope, $routeParams, $timeout,
              Constant, StringUtil, DomUtil, ThemeUtil, TemplateOpt,
              StoreOpt, Category, ProductOpt, Product) {

        $scope.categoryPropMap = {
            title: 'record.name'
        };
        $scope.productPropMap = {
            title: 'record.name',
            image: 'record.thumb'
        };
        $scope.imagePropMap = {
            title: 'record.name',
            image: 'record.url'
        };

        $scope.reset = function() {
            $scope.categoryVo = undefined;
            $scope.productVo = undefined;
            page = 1;

            code = Constant.HTTP_RETURN_CODE_SUCCESS;
        }
        $scope.$on('store_index', function(event, args) {
            $scope.reset();
            $scope.showCategoryList();
        });

        $scope.$on('category_reset', function(event, args) {
            $scope.reset();
            $scope.showCategoryList();
        });
        $scope.$on('category_next_page', function(event, args) {
            page++;
            $scope.showCategoryList();
        });
        $scope.$on('category_select', function(event, item) {
            $scope.reset();
            $scope.showProductList(item);
        });

        $scope.$on('product_reset', function(event, args) {
            $scope.reset();
            $scope.showProductList();
        });
        $scope.$on('product_next_page', function(event, args) {
            page++;
            $scope.showProductList();
        });
        $scope.$on('product_select', function(event, item) {
            $scope.reset();
            $scope.showProductDetail(item);
        });

        $scope.showCategoryList = function() {
            $scope.storeContent = viewPath + 'category-list.html';
            var json = StoreOpt.opt({action: 'queryStoreBySubId', subId: $rootScope.CurrentSub.id, page: page},function() {
                $scope.categoriesVo = {code: 'category', data: json.data.categories, status: json.code};
            });
        }
        $scope.showProductList = function(item) {
            $scope.storeContent = viewPath + 'product-list.html';
            if (item) {
                $scope.currCategory = item;
            }

            var json = ProductOpt.opt({action: 'queryProductByCategoryId', categoryId: $scope.currCategory.id, page: page},function() {
                $scope.productsVo = {code: 'product', data: json.data.products, status: json.code};
            });
        }

        $scope.showProductDetail = function(product) {
            $timeout(function() {
                $scope.storeContent = viewPath + 'product-detail.html';
            }, 10);
            $scope.productVo = {code: 'product', data: product};
        }

        var page = 1, code;
        var viewPath = '/ext/components/store/client/layouts/01/view/';
        $scope.showCategoryList();
    }]);






